//
//  PaperCardController.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/8/2.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperCardController.h"


@interface PaperCardController ()<UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)PaperModel *paper;

@end

@implementation PaperCardController
{
    NSInteger num;
    ClickCellBlock _clickedCell;
}

static NSString * const reuseIdentifier = @"PaperCardCell";

-(instancetype)initWithPaperModel: (PaperModel*)paper
                      clickedCell: (ClickCellBlock)clickedCell{
    
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    if (self = [super initWithCollectionViewLayout:layout]) {
        self.paper = paper;
        _clickedCell = clickedCell;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"选项卡";
    
    [self.collectionView registerClass:[PaperCardCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[PaperCardHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PaperCardHeaderView"];
    
    [self.collectionView reloadData];
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSInteger count = _isCollectQuestionsScanning ? _paper.collectedItems.count : _paper.items.count;
    return count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    PaperItem *item = _isCollectQuestionsScanning ? _paper.collectedItems[section] : _paper.items[section];
    return _isCollectQuestionsScanning ? item.collectedQuestions.count : item.questions.count;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    PaperCardHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"PaperCardHeaderView" forIndexPath:indexPath];
    PaperItem *item = [self paperItemForSection:indexPath.section];
    if (_isCollectQuestionsScanning) {
        view.label.text = [[item.typeName componentsSeparatedByString:@"（"] firstObject];
    }
    else{
        view.label.text = item.typeName;
    }
    return view;
}

// cell size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"submited: %d, collected: %d, index: %ld",q.submited,q.collected,indexPath.row);
    return CGSizeMake(44, 44);
}

// section height
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 30);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PaperCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    PaperQuestion *q = [self paperQuestionAtIndexPath:indexPath];
    cell.finished = q.userAnswer;
    cell.questionNum = indexPath.item+1;
    cell.button.tag = num + 1000;
    [cell.button addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    num += 1;
    return cell;
}

-(void)cellButtonClicked: (UIButton*)sender{
    NSInteger index = sender.tag - 1000;
    _clickedCell(index);
    [self.navigationController popViewControllerAnimated:true];
}

// item
-(PaperItem*)paperItemForSection: (NSInteger)section{
    return _isCollectQuestionsScanning ? _paper.collectedItems[section] : _paper.items[section];
}

// question
-(PaperQuestion*)paperQuestionAtIndexPath: (NSIndexPath*)indexPath{
    PaperItem *item = [self paperItemForSection:indexPath.section];
    return _isCollectQuestionsScanning ? item.collectedQuestions[indexPath.item] : item.questions[indexPath.item];
}

@end


